import { loginResponseDate } from "./vo"
import ajax from "../../../modules/ajax";
import config from "../../../config.json"

export async function SendReq(values: any): Promise<loginResponseDate> {
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/user/login", {
            method: "POST",
            body: JSON.stringify({
                username: values.username,
                password: values.password,
            }),
            headers: {
                "Content-Type": "application/json",
            },
            

        });
        if(res.header("token")) {
            sessionStorage.setItem("token", res.header("token") as string);
            ajax.config.headers = {
                "token": res.header("token")
            }
        }
        console.log(res);
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");

}
export async function SendReqReg(values: any): Promise<loginResponseDate> {

    let res = await ajax.ajax(config.requestUrl+"/gapi/user/logon", {
        method: "POST",
        body: JSON.stringify({
            username: values.username,
            password: values.password,
        }),
        headers: {
            "Content-Type": "application/json;charset=UTF-8",
        }

    });
    if (!res.ok) {
        return res.json()
    }
    return res.json();
}
